#*******************************************************************************
#*** Friendly Fire ***
#*******************************************************************************

# ** Replicate Figure 1 **

#Install/load required packages
install.packages('Rcpp', dependencies = TRUE)
install.packages('ggplot2', dependencies = TRUE)
install.packages('readstata13', dependencies = TRUE)
install.packages('data.table', dependencies = TRUE)
install.packages('gridExtra', dependencies = TRUE)
install.packages('gtable', dependencies = TRUE)
install.packages('grid', dependencies = TRUE)

library(foreign)
library(Rcpp)
library(ggplot2)
library(readstata13)
library(gridExtra)
library(gtable)
library(grid)
rm(list = ls())

setwd("X:/MediasideSupply/Drafts/02 Friendly fire/06 ReResubmission/Research & Politics/R2/replication/") #set custom working directory

tonality<- read.dta13("friendlyfire_replication_R2_agg.dta")

attach(tonality)
tonality$subj_f <- tonality$subj
tonality$obj_f <- tonality$obj

tonality2 <- tonality[order(year, subj, obj ),]

# Party labels
oldvalues <- c(1, 2, 3, 4, 5, 6)
newvalues <- factor(c("SP�","�VP", "FP�", "Greens", "BZ�", "TS"))  # Make this a factor
tonality2$subj_f <- newvalues[ match(tonality2$subj_f, oldvalues) ]
tonality2$obj_f <- newvalues[ match(tonality2$obj_f, oldvalues)]
tonality2$obj_gov <- 0
tonality2$subj_gov <- 0
tonality2$obj_gov[(tonality2$obj_f == "�VP" | tonality2$obj_f == "FP�") & tonality2$year == 2002] <- 1
tonality2$subj_gov[(tonality2$subj_f == "�VP" | tonality2$subj_f == "FP�") & tonality2$year == 2002] <- 1
tonality2$obj_gov[(tonality2$obj_f == "�VP" | tonality2$obj_f == "BZ�") & tonality2$year == 2006] <- 1
tonality2$subj_gov[(tonality2$subj_f == "�VP"  | tonality2$subj_f == "BZ�") & tonality2$year == 2006] <- 1
tonality2$obj_gov[(tonality2$obj_f == "�VP" | tonality2$obj_f == "SP�") & tonality2$year %in% c(2008,2013)] <- 1
tonality2$subj_gov[(tonality2$subj_f == "�VP" | tonality2$subj_f == "SP�") & tonality2$year %in% c(2008,2013)] <- 1

# Subsets
sp02 <- subset( tonality2, subj==1 & year==2002 )
vp02 <- subset( tonality2, subj==2 & year==2002 )
fp02 <- subset( tonality2, subj==3 & year==2002 )
gr02 <- subset( tonality2, subj==4 & year==2002 )

nr02 <- subset( tonality2, year==2002 )
nr06 <- subset( tonality2, year==2006 )
nr08 <- subset( tonality2, year==2008 )
nr13 <- subset( tonality2, year==2013 )


subj_names <- list(
  'subj#1'="SP�",
  'subj#2'="�VP",
  'subj#3'="FP�",
  'subj#4'="Greens",
  'subj#5'="BZ�",
  'subj#6'="Team Stronach"
)
subj_labeller <- function(variable,value){
  return(subj_names[value])
}

# ADDITIONAL FUNCTIONS

gtable_select <- function (x, ...) 
{
  matches <- c(...)
  x$layout <- x$layout[matches, , drop = FALSE]
  x$grobs <- x$grobs[matches]
  x
}
gtable_stack <- function(g1, g2){
  g1$grobs <- c(g1$grobs, g2$grobs)
  g1$layout <- transform(g1$layout, z= z-max(z), name="g2")
  g1$layout <- rbind(g1$layout, g2$layout)
  g1
}

# PLOT THE DATA USING GGPLOT2

# 02

gr_facet_nr02 <- ggplot(data = nr02, aes(x = obj, y = satzscore_dsk, group = obj_gov)) +
  geom_point(aes(size = freq, colour = factor(obj_gov))) +
  scale_size(range = c(2,8.5)) +
  scale_colour_manual(values = c("grey80","grey50")) +
  geom_point(aes(size = freq), shape = 21) +
  geom_point(shape = 20, colour = "black", size = 0.5) +
  theme_bw() +
  scale_x_continuous(name = "Number of attacks", 
                   limits = c(0.5,6),
                   breaks = c(1,2,3,4,5,6), 
                   labels = c("SP�","�VP","FP�","Gr",".",".")) +
  scale_y_continuous(name = "Mean tonality",
                     limits = c(0,4),
                     breaks = c(0,1,2,3,4)) +
  facet_wrap(~ subj, labeller = subj_labeller, ncol = 4) +
  theme(legend.position = "none",
        strip.text.x = element_text(size=11, face="bold"),
        strip.text.y = element_text(size=5)) + 
  ggtitle(2002)
dummy <- ggplot(data = nr02, aes(x = obj, y = satzscore_dsk, group = obj_gov)) +
  facet_wrap(~ subj, labeller = subj_labeller, ncol = 4) + 
  geom_rect(aes(fill=factor(subj_gov)), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
  scale_fill_manual(values = c("grey80","grey50")) +
  theme_minimal() +
  theme(strip.text.x = element_text(size = 11, face = "bold"))

g1 <- ggplotGrob(gr_facet_nr02)
g2 <- ggplotGrob(dummy)
panels <- grepl(pattern="panel", g2$layout$name)
strips <- grepl(pattern="strip-t", g2$layout$name)
g2$layout$t[panels] <- g2$layout$t[panels] - 1
g2$layout$b[panels] <- g2$layout$b[panels] - 1
new_strips <- gtable_select(g2, panels | strips)
gr_facet_nr02 <- gtable_stack(g1, new_strips)

png("gr_facet_nr02.png", width = 11, height = 3, units = "in", res = 300)
grid.draw(gr_facet_nr02)
dev.off()

# 06

gr_facet_nr06 <- ggplot(data = nr06, aes(x = obj, y = satzscore_dsk, group = obj_gov)) +
  geom_point(aes(size = freq, colour = factor(obj_gov))) +
  scale_size(range = c(2,8.5)) +
  scale_colour_manual(values = c("grey80","grey50")) +
  geom_point(aes(size = freq), shape = 21) +
  geom_point(shape = 20, colour = "black", size = 0.5) +
  theme_bw() +
  scale_x_continuous(name = "Number of attacks", 
                     limits = c(1,6),
                     breaks = c(1,2,3,4,5,6), 
                     labels = c("SP�","�VP","FP�","Gr","BZ�",".")) +
  scale_y_continuous(name = "Mean tonality",
                     limits = c(0,4),
                     breaks = c(0,1,2,3,4)) +
  facet_wrap(~ subj, labeller = subj_labeller, ncol = 5) +
  theme(legend.position = "none",
        strip.text.x = element_text(size=11, face="bold"),
        strip.text.y = element_text(size=5)) + 
  ggtitle(2006)
dummy <- ggplot(data = nr06, aes(x = obj, y = satzscore_dsk, group = obj_gov)) +
  facet_wrap(~ subj, labeller = subj_labeller, ncol = 5) + 
  geom_rect(aes(fill=factor(subj_gov)), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
  scale_fill_manual(values = c("grey80","grey50")) +
  theme_minimal() +
  theme(strip.text.x = element_text(size = 11, face = "bold"))

g1 <- ggplotGrob(gr_facet_nr06)
g2 <- ggplotGrob(dummy)
panels <- grepl(pattern="panel", g2$layout$name)
strips <- grepl(pattern="strip-t", g2$layout$name)
g2$layout$t[panels] <- g2$layout$t[panels] - 1
g2$layout$b[panels] <- g2$layout$b[panels] - 1
new_strips <- gtable_select(g2, panels | strips)
gr_facet_nr06 <- gtable_stack(g1, new_strips)

png("gr_facet_nr06.png", width = 11, height = 3, units = "in", res = 300)
grid.draw(gr_facet_nr06)
dev.off()

# 08

gr_facet_nr08 <- ggplot(data = nr08, aes(x = obj, y = satzscore_dsk, group = obj_gov)) +
  geom_point(aes(size = freq, colour = factor(obj_gov))) +
  scale_size(range = c(2,6.5)) +
  scale_colour_manual(values = c("grey80","grey50")) +
  geom_point(aes(size = freq), shape = 21) +
  geom_point(shape = 20, colour = "black", size = 0.5) +
  theme_bw() +
  scale_x_continuous(name = "Number of attacks", 
                     limits = c(0.5,6),
                     breaks = c(1,2,3,4,5,6), 
                     labels = c("SP�","�VP","FP�","Gr","BZ�",".")) +
  scale_y_continuous(name = "Mean tonality",
                     limits = c(0,4),
                     breaks = c(0,1,2,3,4)) +
  facet_wrap(~ subj, labeller = subj_labeller, ncol = 5) +
  theme(legend.position = "none",
        strip.text.x = element_text(size=11, face="bold"),
        strip.text.y = element_text(size=5)) + 
  ggtitle(2008)
dummy <- ggplot(data = nr08, aes(x = obj, y = satzscore_dsk, group = obj_gov)) +
  facet_wrap(~ subj, labeller = subj_labeller, ncol = 5) + 
  geom_rect(aes(fill=factor(subj_gov)), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
  scale_fill_manual(values = c("grey80","grey50")) +
  theme_minimal() +
  theme(strip.text.x = element_text(size = 11, face = "bold"))

g1 <- ggplotGrob(gr_facet_nr08)
g2 <- ggplotGrob(dummy)
panels <- grepl(pattern="panel", g2$layout$name)
strips <- grepl(pattern="strip-t", g2$layout$name)
g2$layout$t[panels] <- g2$layout$t[panels] - 1
g2$layout$b[panels] <- g2$layout$b[panels] - 1
new_strips <- gtable_select(g2, panels | strips)
gr_facet_nr08 <- gtable_stack(g1, new_strips)

png("gr_facet_nr08.png", width = 11, height = 3, units = "in", res = 300)
grid.draw(gr_facet_nr08)
dev.off()

# 13

gr_facet_nr13 <- ggplot(data = nr13, aes(x = obj, y = satzscore_dsk, group = obj_gov)) +
  geom_point(aes(size = freq, colour = factor(obj_gov))) +
  scale_size(range = c(2,8.5)) +
  scale_colour_manual(values = c("grey80","grey50")) +
  geom_point(aes(size = freq), shape = 21) +
  geom_point(shape = 20, colour = "black", size = 0.5) +
  theme_bw() +
  scale_x_continuous(name = "Number of attacks", 
                     limits = c(0.5,6),
                     breaks = c(1,2,3,4,5,6), 
                     labels = c("SP�","�VP","FP�","Gr","BZ�","TS")) +
  scale_y_continuous(name = "Mean tonality",
                     limits = c(0,4),
                     breaks = c(0,1,2,3,4)) +
  facet_wrap(~ subj, labeller = subj_labeller, ncol = 6) +
  theme(legend.position = "none",
        strip.text.x = element_text(size=11, face="bold"),
        strip.text.y = element_text(size=5)) + 
  ggtitle(2013)
dummy <- ggplot(data = nr13, aes(x = obj, y = satzscore_dsk, group = obj_gov)) +
  facet_wrap(~ subj, labeller = subj_labeller, ncol = 6) + 
  geom_rect(aes(fill=factor(subj_gov)), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
  scale_fill_manual(values = c("grey80","grey50")) +
  theme_minimal() +
  theme(strip.text.x = element_text(size = 11, face = "bold"))

g1 <- ggplotGrob(gr_facet_nr13)
g2 <- ggplotGrob(dummy)
panels <- grepl(pattern="panel", g2$layout$name)
strips <- grepl(pattern="strip-t", g2$layout$name)
g2$layout$t[panels] <- g2$layout$t[panels] - 1
g2$layout$b[panels] <- g2$layout$b[panels] - 1
new_strips <- gtable_select(g2, panels | strips)
gr_facet_nr13 <- gtable_stack(g1, new_strips)

png("gr_facet_nr13.png", width = 11, height = 3, units = "in", res = 300)
grid.draw(gr_facet_nr13)
dev.off()

